﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tab2Sql.Classes.Core
{
	/// <summary>
	/// Helper class. Static methods for mangling column names. 
	/// </summary>
    public class TsvHelper
    {
		/// <summary>
		/// Adjusts supplied column name according to which ruleset is supplied. See HandlingOfColumnNames enum for more
		/// info.
		/// </summary>
		/// <param name="columnName">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="columnNameSettings">
		/// A <see cref="HandlingOfColumnNames"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
        public  static string ColumnNameAdjustment(string columnName, HandlingOfColumnNames columnNameSettings)
        {
           
            switch (columnNameSettings)
            {
                case HandlingOfColumnNames.NoChanges:
                    // foo.
                    break;
                case HandlingOfColumnNames.JustAddQuotes:
                    columnName = "[" + columnName + "]"; //TODO - At present quotes are assumed to be hard brackets. Need to add in option to allow quotes to be actual quotes or whatever arbritrary word boundaries needed (E.g. MySql backticks)
                    break;
                case HandlingOfColumnNames.Standard:
                    columnName = fixColumnHeader(columnName);

                    break;
                default:

                    break;
            }
            return columnName;
        }








        private static string fixColumnHeader(string columnName)
        {
            columnName = columnName.Trim(new char[] { '"', ' ', '[', ']', '\r', '\n' });
            var undesirableChars = new char[11];
            undesirableChars[0] = '/';
            undesirableChars[1] = '-';
            undesirableChars[2] = '[';
            undesirableChars[3] = ']';
            undesirableChars[4] = '#';
            undesirableChars[5] = '"';
            undesirableChars[6] = '\t';
            undesirableChars[7] = '\n';
            undesirableChars[8] = '\r';
            undesirableChars[9] = '.';
            undesirableChars[10] = ',';

            foreach (char udc in undesirableChars)
            {
                columnName = columnName.Replace(udc.ToString(), string.Empty);
            }
            columnName = columnName.Substring(0, Math.Min(columnName.Length, 35));
            columnName = columnName.Replace(" ", "_");
            return columnName;
        }




    }
}
